#include "tcpengine_plugin.h"
#include "FileHandler.h"
#include "transfersocket.h"
#include <QDebug>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QMutex>


void registerTypes(const char *uri)
{
    qmlRegisterType<TransferSocket>("TransferSocket",1,0,"TransferSocket");
    qmlRegisterType<TcpEnginePlugin>(uri, 1, 0, "TcpEngine");
    qmlRegisterSingletonInstance("FileHandler", 1, 0, "FileHandler", FileHandler::instance());
}

void TcpEnginePlugin::registerTypes(const char *uri)
{
    // @uri com.mycompany.qmlcomponents
    ::registerTypes(uri);
}

static PluginInfo *info;

void releasePluginInfo()
{
    delete info->m_subWin;
    delete info->m_chiBtn;
    delete info;
}

PluginInfo *getPluginInfo(QQmlApplicationEngine* engine)
{
    ::registerTypes("TcpEngine");
    QQmlComponent* subWin = new QQmlComponent(engine, QUrl("qrc:/TcpEngine.qml"));
    QQmlComponent* chiBtn = new QQmlComponent(engine, QUrl("qrc:/TcpButton.qml"));
    QObject* objSubWin = subWin->create();
    QObject* objChiBtn = chiBtn->create();
    if (objSubWin && objChiBtn)
    {
        info = new PluginInfo{
                .m_name = "文件传输页面",
                .m_subWin = dynamic_cast<QQmlComponent*>(objSubWin),
                .m_chiBtn = dynamic_cast<QQmlComponent*>(objChiBtn)
            };
    }
    else
    {
        if (subWin->isError())
            qCritical() << subWin->errorString().toStdString().data();
        if (chiBtn->isError())
            qCritical() << chiBtn->errorString().toStdString().data();
        info = nullptr;
    }
    return info;
}

